<?php
namespace BaseBundle\Model;

use RedUnicorn\SymfonyKernel\Model\Model;
use Symfony\Component\DependencyInjection\ContainerInterface;
use RedUnicorn\Validate\Validate;
use BaseBundle\Entity\ArticleClass;

class m_article_class extends Model 
{

    /**
     * @var ArticleClass
     */
    private $article_class_class = null;

    function __construct(ContainerInterface $container)
    {   

        parent::__construct($container, 'article_class');
    }
 
    public function validate($data, $id = false)
    {
        $this->entityManager = $this->getDoctrine()->getManager();
        
        if(empty($id)){
            $this->article_class_class = new ArticleClass();
        }else{
            if(!$this->is_exist($id)){
                self::$error->setError($this->get('translator')->trans('未找到对应信息'));
                return false;
            }

            $this->article_class_class = $this->entityManager->getRepository(ArticleClass::class)->find($id);
        }
                
        if(array_key_exists('article_class_name', $data)){
            $this->article_class_class->setArticleClassName($data['article_class_name']);
        }
                
        if(array_key_exists('is_del', $data)){
            $this->article_class_class->setIsDel($data['is_del']);
        }
                
        if(array_key_exists('is_recommend', $data)){
            $this->article_class_class->setIsRecommend($data['is_recommend']);
        }

        if(array_key_exists('is_built', $data)){
            $this->article_class_class->setIsBuilt($data['is_built']);
        }
                
        if(array_key_exists('show_image', $data)){
            $this->article_class_class->setShowImage($data['show_image']);
        }

        if(array_key_exists('view_times', $data)){
            $this->article_class_class->setViewTimes($data['view_times']);
        }

        if(self::$error->validate($this->article_class_class)){
            return false;
        }
        
        return true;
    }
    
    public function insert($data)
    {
        if(!$this->validate($data)){
            return false;
        }
        
        
        $this->entityManager->persist($this->article_class_class);
        $this->entityManager->flush();
    
        $this->lastInsertId = $this->article_class_class->getId();

        return true;
    }
    
    public function update($id ,$data)
    {
        if(empty($id)){
            self::$error->setError('param id is not null');
            return false;
        }
        
        if(!$this->validate($data, $id)){
            return false;
        }
        
        $this->entityManager->flush();

        return true;
    }
    
    public function rewritesRule(&$rule)
    {   
                    
        //where   
        if(array_key_exists('id', $rule) && !empty($rule['id'])){
           $this->sql_array['where'] .= " AND sql_pre.id = '{$rule['id']}' ";
        }
                    
        //where   
        if(array_key_exists('article_class_name', $rule) && !Validate::isRealEmpty($rule['article_class_name'])){
            $this->sql_array['where'] .= " AND sql_pre.article_class_name LIKE '{$rule['article_class_name']}' ";
        }

        //where 分类名称模糊查询
        if(array_key_exists('article_class_name_like', $rule) && !Validate::isRealEmpty($rule['article_class_name_like'])){
            $this->sql_array['where'] .= " AND sql_pre.article_class_name LIKE '%{$rule['article_class_name_like']}%' ";
        }

        //where   
        if(array_key_exists('is_del', $rule)){
           $this->sql_array['where'] .= " AND sql_pre.is_del = '{$rule['is_del']}' ";
        }else{
            $this->sql_array['where'] .= " AND sql_pre.is_del = '0' ";
        }

        //where   
        if(array_key_exists('is_recommend', $rule) && $rule['is_recommend'] !== '' && $rule['is_recommend'] != '-1'){
           $this->sql_array['where'] .= " AND sql_pre.is_recommend = '{$rule['is_recommend']}' ";
        }

        //where
        if(array_key_exists('is_built', $rule)){
            $this->sql_array['where'] .= " AND sql_pre.is_built = '{$rule['is_built']}' ";
        }

        //where   
        if(array_key_exists('show_image', $rule) && !Validate::isRealEmpty($rule['show_image'])){
            $this->sql_array['where'] .= " AND sql_pre.show_image LIKE '{$rule['show_image']}' ";
        }
    }


    public static function joinInfo(Model $model, &$rule)
    {
        parent::joinInfo($model, $rule);

        if (array_key_exists('join_article_class', $rule)){
            $model->sql_array['join'] .=  "LEFT JOIN article_class ac ON ac.id = {$model->sql_array['pre']}.article_class_id";
        }

        if (array_key_exists('select_article_class_msg', $rule) && array_key_exists('join_article_class', $rule)){
            $model->sql_array['select'] .=  ",ac.article_class_name";
        }

    }

    public function setInfo($info)
    {
        parent::setInfo($info);

        if (array_key_exists('is_recommend',$info)){
            $info['is_recommend_str'] = $this->get('unicorn.array')->value($this->getParameter('is_true_arr'), $info['is_recommend']);
        }

        if (array_key_exists('show_image',$info)){
            $info['show_image_path'] = $this->get('unicorn.files')->getFilePath($info['show_image'],true);
        }

        return $info;
    }

    public function builtInData()
    {
        $data = [
            [
                'id' => $this->getParameter('default_article_classes_id'),
                'article_class_name' => '其他',
                'show_image' => $this->getParameter('default_other_article_classes_image'),
                'is_built' => '1',
                'is_recommend' => '0'
            ],
            [
                'id' => 2,
                'article_class_name' => 'Nightlife',
                'show_image' => $this->getParameter('default_nightlife_article_classes_image'),
                'is_built' => '1',
                'is_recommend' => '1'
            ],
            [
                'id' => 3,
                'article_class_name' => 'Restaurants',
                'show_image' => $this->getParameter('default_restaurants_article_classes_image'),
                'is_built' => '1',
                'is_recommend' => '1'
            ],
            [
                'id' => 4,
                'article_class_name' => 'Outdoors',
                'show_image' => $this->getParameter('default_outdoors_article_classes_image'),
                'is_built' => '1',
                'is_recommend' => '1'
            ],
            [
                'id' => 5,
                'article_class_name' => 'Hotels',
                'show_image' => $this->getParameter('default_hotels_article_classes_image'),
                'is_built' => '1',
                'is_recommend' => '1'
            ],
            [
                'id' => 6,
                'article_class_name' => 'Art & Culture',
                'show_image' => $this->getParameter('default_art_culture_article_classes_image'),
                'is_built' => '1',
                'is_recommend' => '1'
            ]
        ];

        foreach ($data as $k=>$v){
            self::$conn->insert($this->getTableName(),$v);
        }
    }
}